/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.view;

import com.minierp.model.BusinessBill;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.Region;
import org.springframework.web.servlet.view.document.AbstractExcelView;

public class BusinessBillListForCloseExcelView extends AbstractExcelView {
	public void buildExcelDocument(Map model, HSSFWorkbook workbook,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		response.reset();
		response.setContentType("APPLICATION/vnd.ms-excel");
		response.setHeader("Content-Disposition",
				"attachment;filename=\"BusinessBillListForClose.xls\"");
		HSSFSheet sheet = workbook.createSheet("list");
		HSSFRow r = sheet.createRow(0);
		HSSFCell c = r.createCell(0);
		r.createCell(1);
		r.createCell(2);
		r.createCell(3);
		r.createCell(4);
		r.createCell(5);
		r.createCell(6);
		r.createCell(7);
		r.createCell(8);
		r.createCell(9);
		r.createCell(10);
		c.setCellValue(new HSSFRichTextString("金额合计：" + model.get("costTotal")));
		sheet.addMergedRegion(new Region(0, 0, 0, 10));
		r = sheet.createRow(1);
		r.createCell(0).setCellValue(new HSSFRichTextString("业务单号"));
		r.createCell(1).setCellValue(new HSSFRichTextString("订货日期"));
		r.createCell(2).setCellValue(new HSSFRichTextString("订货单位"));
		r.createCell(3).setCellValue(new HSSFRichTextString("摘要"));
		r.createCell(4).setCellValue(new HSSFRichTextString("金额"));
		r.createCell(5).setCellValue(new HSSFRichTextString("调账"));
		r.createCell(6).setCellValue(new HSSFRichTextString("交货日期"));
		r.createCell(7).setCellValue(new HSSFRichTextString("最近回款日期"));
		r.createCell(8).setCellValue(new HSSFRichTextString("跟单员"));
		r.createCell(9).setCellValue(new HSSFRichTextString("完工状态"));
		r.createCell(10).setCellValue(new HSSFRichTextString("送货状态"));
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
		List list = (List) model.get("list");

		for (int os = 0; os < list.size(); ++os) {
			Object[] objs = (Object[]) list.get(os);
			BusinessBill b = (BusinessBill) objs[0];
			String deliDate = "";
			String receDate = "";
			String compStat = "";
			String deliStat = "";
			if (b.getDeliverDate() != null) {
				deliDate = sf.format(b.getDeliverDate());
			}

			if (objs[2].toString().equals("Y")) {
				compStat = "已完工";
			} else {
				compStat = "未完工";
			}

			if (objs[3].toString().equals("Y")) {
				deliStat = "已送完";
			} else if (objs[3].toString().equals("P")) {
				deliStat = "未送完";
			} else {
				deliStat = "未送货";
			}

			if (objs[5] != null) {
				receDate = sf.format((Date) objs[5]);
			}

			r = sheet.createRow(os + 2);
			r.createCell(0).setCellValue(
					new HSSFRichTextString(b.getBillCode()));
			r.createCell(1).setCellValue(
					new HSSFRichTextString(sf.format(b.getOrderDate())));
			r.createCell(2).setCellValue(
					new HSSFRichTextString((String) objs[1]));
			r.createCell(3)
					.setCellValue(new HSSFRichTextString(b.getContent()));
			r.createCell(4).setCellValue(b.getCost().doubleValue());
			r.createCell(5).setCellValue(((BigDecimal) objs[4]).doubleValue());
			r.createCell(6).setCellValue(new HSSFRichTextString(deliDate));
			r.createCell(7).setCellValue(new HSSFRichTextString(receDate));
			r.createCell(8).setCellValue(
					new HSSFRichTextString(b.getFollowerNameStr()));
			r.createCell(9).setCellValue(new HSSFRichTextString(compStat));
			r.createCell(10).setCellValue(new HSSFRichTextString(deliStat));
		}

		ServletOutputStream arg16 = response.getOutputStream();
		workbook.write(arg16);
		arg16.flush();
		arg16.close();
	}
}